package cn.trigram.mgb.api.java;

import java.sql.Types;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl;

public class JavaTypeResolverImpl extends JavaTypeResolverDefaultImpl {


  @Override
  public FullyQualifiedJavaType calculateJavaType(
      IntrospectedColumn introspectedColumn
  ) {

    int    jdbcType         = introspectedColumn.getJdbcType();
    String actualColumnName = StringUtils.lowerCase(introspectedColumn.getActualColumnName());
    if (jdbcType == Types.BIGINT && StringUtils.endsWithAny(actualColumnName, "time", "date")) {
      return new FullyQualifiedJavaType(Date.class.getName());
    } else if (jdbcType == Types.TINYINT) {
      return new FullyQualifiedJavaType(Integer.class.getName());
    } else if(jdbcType==Types.BIGINT){
      return new FullyQualifiedJavaType(Long.class.getName());
    }
    return super.calculateJavaType(introspectedColumn);
  }

}
